<?php
/**
 * Created by zengwei.
 * Date: 2019/10/15
 * Time: 16:32
 * Function:
 */

namespace App\Utilities;


use App\Models\Cafe;
use App\Models\Tag;

class Tagger
{
    /**
     * Created by zengwei.
     * Date: 2019/10/15
     * Time: 16:41
     * Function:为cafe添加标签
     * @param Cafe $cafe
     * @param array $tags
     * @param int $userId
     *
     */
    public static function tagCafe(Cafe $cafe,array $tags,int $userId){
        foreach ($tags as $tag){
            $tagItem = Tag::firstOrCreate(['name'=>$tag]);
            $cafe->tags()->syncWithoutDetaching([$tagItem->id=>['user_id'=>$userId]]);
        }
    }

}
